package com.example.java_gobang.interceptor;

import com.example.java_gobang.contant.Contants;
import com.example.java_gobang.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
@Configuration
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //校验token
        String token = request.getHeader(Contants.USER_TOKEN_HANDER);
        log.info("传过来的token：{}",token);
        Claims claims = JwtUtils.parseToken(token);
        if(claims == null){
            System.out.println(401);
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
